package com.shushan.pages;

import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

import com.shushan.base.ActivityStackManager;
import com.shushan.base.Constants;
import com.shushan.base.NavigationLoseActivity;
import com.shushan.permission.OpenUsageActivity;
import com.shushan.service.DialRTAService;
import com.shushan.shushanbao.R;
import com.shushan.ui.CustomDialog;
import com.shushan.util.DialogUtil;
import com.shushan.util.SharedPreferencesUtils;
import com.shushan.util.SystemUtils;

/**
 * @ClassName: UserServiceCenterActivity
 * @Desciption: //客服中心界面
 * @author: zhangshihao
 * @date: 2018-09-21
 */
public class UserServiceCenterActivity extends NavigationLoseActivity implements View.OnClickListener{

    private static final String TAG = UserServiceCenterActivity.class.getSimpleName();

    private ImageView ivCall;
    private Button btnCopy;

    private ClipboardManager clipboardManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user_service_center);

        setTitle("客服中心");

        initView();

    }

    private void initView(){
        ivCall = findViewById(R.id.iv_call);
        ivCall.setOnClickListener(this);
        btnCopy = findViewById(R.id.btn_copy);
        btnCopy.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.iv_call:
                dialCustom();
                break;
            case R.id.btn_copy:
                if(clipboardManager == null) {
                    clipboardManager = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
                }
                ClipData myClip;
                myClip = ClipData.newPlainText("gzhName", "shushanzhixue");
                clipboardManager.setPrimaryClip(myClip);
                showToast("复制成功");
                break;
        }
    }

    private void dialCustom() {
        boolean isChildControl = SharedPreferencesUtils.getBooleanDate(Constants.IS_CHILD_CONTROL_SHAREPREFERS_KEY);
        if(isChildControl && !SystemUtils.checkUsagePermission(this)){
            showNoUsageDialog();
            return;
        }

        try {
            Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + Constants.CLIENT_PHONE_NUM));
            startActivityAllow(intent);
            DialogUtil.dismissDialog();
            DialogUtil.dismissParentSetDialog();
        }catch (SecurityException se){
            se.printStackTrace();
            showToast("没有打电话权限");
            return;
        }

        if(isChildControl){
            setAllowBack(true);
            ActivityStackManager.getActivityStackManager().popAllActivity();
            Intent intent = new Intent(this,DialRTAService.class);
            intent.putExtra("allowPackName",Constants.ALLOW_DIAL_PACK_NAME);
            startService(intent);
        }
    }

    private void showNoUsageDialog() {
        new CustomDialog.Builder(this)
                .setTitle("温馨提示")
                .setMessage("发现您尚未开启“有权查看应用使用情况”，将导致无法返回首页，是否去设置？")
                .setNegativeButton("放弃", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();

                    }
                })
                .setPositiveButton("设置", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();

                        startActivity(OpenUsageActivity.class);
                    }
                })
                .create().show();
    }

}
